package com.homegoods.home.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.home.R;
import com.homegoods.home.databinding.HomeNewProductLayoutBinding;
import com.homegoods.home.model.NewProductModel;
import com.homegoods.resource.ILifecycle;
import com.homegoods.resource.SpacesItemDecoration;
import com.homegoods.aroutercomment.ARouterPath;
import com.jakewharton.rxbinding2.view.RxView;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;
import com.kedll.library.util.MyUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @ClassName: NewProductView
 * @date 2018/9/5
 * @description 限时活动界面
 */
public class NewProductView extends FrameLayout implements ILifecycle {
    private HomeNewProductLayoutBinding mBind;
    private MultiTypeAdapter newProductAdapter;
    private Disposable clickDispose, newProductDispose;
    private String childSid;


    public NewProductView(@NonNull Context context) {
        this(context, null);
    }

    public NewProductView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NewProductView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mBind = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.home_new_product_layout, this, true);
        initEvent(context);
    }

    private void initEvent(Context context) {
        //跳转到更多限时活动界面
        MyUtils.dispose(clickDispose);
        clickDispose = RxView.clicks(mBind.atMore)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> ARouter.getInstance().build(ARouterPath.PATH_NEW_PRODUCT).withString("childSid", childSid).navigation());

        mBind.list.setHasFixedSize(true);
        mBind.list.setNestedScrollingEnabled(false);
        mBind.list.setLayoutManager(new GridLayoutManager(getContext(), 1));
        mBind.list.addItemDecoration(new SpacesItemDecoration(2, 8));
        newProductAdapter = new MultiTypeAdapter();

        mBind.list.setAdapter(newProductAdapter);

    }

    /**
     * 设置限时活动数据
     *
     * @param items
     */
    public void setNewProductContent(List<NewProductModel> items, String childSid) {
        this.childSid = childSid;
        MyUtils.dispose(newProductDispose);

        newProductDispose = Flowable.fromIterable(items)
                .subscribeOn(Schedulers.computation())
                .map((Function<NewProductModel, BaseItem>) newProductModel -> newProductModel.createItem(newProductAdapter))
                .toList()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseItems -> newProductAdapter.addAllWithClear(baseItems));
    }


    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroy() {
        if (mBind != null) {
            mBind.unbind();
            mBind = null;
        }

        //取消订阅
        MyUtils.dispose(clickDispose);
        MyUtils.dispose(newProductDispose);
    }

}
